/*
 * Libraries for J2ME (Java mobile) portable devices such as cellular phones.
 * Copyright (c) 2005-2006, Moshe Waisberg. All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contributors can be contacted by electronic mail via the project Web pages:
 * 
 * https://sourceforge.net/projects/j2melib
 * 
 * http://j2melib.sourceforge.net
 * 
 */
package games;

//import net.sf.j2me.lcdui.game.GameBoard;

/**
 * Represents a hop from one cell to another cell.
 * 
 * @author Moshe Waisberg
 */
public class Hop {

    /**
     * No hop.
     */
    public static final int HOP_NONE = 0x0000;
    /**
     * Valid hop?
     */
    public static final int HOP_VALID = 0x0003;
    /**
     * Hop by black marker?
     */
    public static final int HOP_BLACK = 0x0004;
    /**
     * Reserved.
     */
    public static final int HOP_RESERVED1 = 0x0008;
    /**
     * Hop 2 cells over opponent, killing opponent's marker?
     */
    public static final int HOP_KILL = 0x0010;
    /**
     * Hop is to the right?
     */
    public static final int HOP_RIGHT = 0x0020;
    /**
     * Hop is downwards?
     */
    public static final int HOP_DOWN = 0x0040;

    /** The hopping marker. */
    public short marker = Checkers.MARKER_NONE;
    /** Source row index. */
    public int fromRow = -1;
    /** Source column index from. */
    public int fromColumn = -1;
    /** Destination row index. */
    public int toRow = -1;
    /** Destination column index. */
    public int toColumn = -1;
    /** Hop type - bit flags. */
    public int hopType = HOP_NONE;
    /** Next hop. This is hop with the highest total weight, after having alternate hops pruned. */
    public Hop next;
    /** Maximum depth. The maximum number of <em>next</em> hops. */
    public int depth = 0;
    /** Weight value. This is the difference between the current board score, and the board score after this hop. */
    public int weight = 0;
    /** Total weight value - sum of this weight and all <em>next</em> hops. */
    public int weightTotal = 0;

    public Hop() {
        super();
    }

    public boolean equals(Object other) {
        if (this == other)
            return true;

        boolean result = super.equals(other);

        if (other instanceof Hop) {
            Hop that = (Hop) other;
            result = true;
            result &= this.fromRow == that.fromRow;
            result &= this.fromColumn == that.fromColumn;
            result &= this.toRow == that.toRow;
            result &= this.toColumn == that.toColumn;
            result &= this.hopType == that.hopType;
        }

        return result;
    }

    public int hashCode() {
        int from = (((fromRow & 0xFF) << 8) | (fromColumn & 0xFF));
        int to = (((toRow & 0xFF) << 8) | (toColumn & 0xFF));

        return (from << 16) | to;
    }

    /**
     * Print the hop tree.
     */
    public void print() {
        print(0);
    }

    /**
     * Print the hop tree.
     * 
     * @param level
     *            the level of indentation.
     */
    protected void print(int level) {
        StringBuffer buf = new StringBuffer();

        buf.append((int) marker);
        while (buf.length() < 4)
            buf.insert(0, ' ');
        for (int i = 0; i < level; i++)
            buf.insert(0, '~');
        buf.append(',').append(' ').append('[');
        buf.append(fromRow).append(',').append(fromColumn);
        buf.append("] -> [");
        buf.append(toRow).append(',').append(toColumn);
        buf.append(']').append(' ').append('{');

        if ((hopType & HOP_DOWN) != 0)
            buf.append("down");
        else
            buf.append("up");

        if ((hopType & HOP_RIGHT) != 0)
            buf.append(", right");
        else
            buf.append(", left");

        if ((hopType & HOP_KILL) != 0)
            buf.append(", kill");

        buf.append(',').append(' ').append(depth);
        buf.append(',').append(' ').append(weight);
        buf.append(',').append(' ').append(weightTotal);

        buf.append('}');

        System.out.println(buf);

        if (next != null)
            next.print(level + 1);
    }
}